package com.xhorse.lockplatform.comm.util;

import java.math.BigInteger;

public class ByteTransferUtil {

	/**
	 * 字节数组转16进制字符串
	 * 
	 * @param brandName
	 * @return
	 */
	public static String bytesToHexString(byte[] brandName) {
		StringBuilder stringBuilder = new StringBuilder();
		if (brandName == null || brandName.length <= 0) {
			return null;
		}
		for (int i = 0; i < brandName.length; i++) {
			int v = brandName[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}

	/**
	 * 字节数组转10进制
	 * 
	 * @param brandId
	 * @return
	 */
	public static String bytesToOctalString(byte[] brandId) {
		StringBuilder stringBuilder = new StringBuilder();
		if (brandId == null || brandId.length <= 0) {
			return null;
		}
		int mid = brandId.length / 2;
		// 低位与高位进行交换
		byte[] temp = new byte[brandId.length];
		for (int i = 0; i < mid; i++) {
			temp[i] = brandId[i + mid];
			temp[i + mid] = brandId[i];
		}
		// 开始转换
		for (int i = 0; i < temp.length; i++) {
			int v = brandId[i] & 0xFF;
			stringBuilder.append(String.valueOf(v));
		}
		return stringBuilder.toString();
	}

	/**
	 * 单字节转16进制字符
	 * 
	 * @param src
	 * @return
	 */
	public static String byteToHexString(byte src) {
		StringBuilder stringBuilder = new StringBuilder();
		if (src == 0) {
			return null;
		}
		int v = src & 0xFF;
		String temp = Integer.toHexString(v);
		if (temp.length() < 2)
			stringBuilder.append(0);
		stringBuilder.append(temp);
		return stringBuilder.toString();
	}

	/**
	 * 单字节转10进制字符
	 * 
	 * @param src
	 * @return
	 */
	public static String byteToOctalString(byte src) {
		StringBuilder stringBuilder = new StringBuilder();
		if (src == 0) {
			return null;
		}
		int v = src & 0xFF;
		stringBuilder.append(String.valueOf(v));
		return stringBuilder.toString();
	}

	/**
	 * 将byte[]转为各种进制的字符串
	 * 
	 * @param modelName
	 *            byte[]
	 * @param radix
	 *            基数可以转换进制的范围，从Character.MIN_RADIX到Character.MAX_RADIX，
	 *            超出范围后变为10进制
	 * @return 转换后的字符串
	 */
	public static String bytesToString(byte[] modelName, int radix) {
		// 低位与高位进行交换
		byte[] temp = new byte[modelName.length];
		int mid = modelName.length / 2;
		for (int i = 0; i < mid; i++) {
			temp[i] = modelName[i + mid];
			temp[i + mid] = modelName[i];
		}
		// 字节数组转换
		return new BigInteger(1, temp).toString(radix);// 这里的1代表正数
	}

	/**
	 * 16进制字符串转字节数组
	 * @param hexString
	 * @return
	 */
	public static byte[] hexStringToBytes(String hexString) {
		if (hexString == null || hexString.equals("")) {
			return null;
		}
		int length = hexString.length() / 2;
		char[] hexChars = hexString.toCharArray();
		byte[] byteArray = new byte[length];
		for (int i = 0; i < length; i++) {
			int pos = i * 2;
			byteArray[i] = (byte) ((byte) "0123456789ABCDEF"
					.indexOf(hexChars[pos]) << 4 | (byte) "0123456789ABCDEF"
					.indexOf(hexChars[pos + 1]));
		}
		return byteArray;
	}

	/**
	 * 测试
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		byte number = (byte) 0xCD;
		System.out.println(byteToOctalString(number));
	}

}
